/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myfx.jfxpanel;

import Globals.AllGlobal;
import brain.MyBrain;
import component.AWTpicture;
import component.StringToImg;
import controller.MyController;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javax.swing.JOptionPane;
import myfx.component.About;

/**
 *
 * @author Administrator
 */
public class MyMenu extends JFXPanel{
    private int width;
    private int height;
    private MyController controller;
    private MyBrain brain;
    private Color color;
//    String cssStyle1="-fx-background-color:\n" +
//                "        #c3c4c4,\n" +
//                "        linear-gradient(#d6d6d6 50%, white 100%),\n" +
//                "        radial-gradient(center 50% -40%, radius 200%, #e6e6e6 45%, rgba(230,230,230,0) 50%);\n" +
//                "    -fx-background-radius: 30;\n" +
//                "    -fx-background-insets: 0,30,0,30;\n" +
//                "    -fx-text-fill: black;\n" +
//                "    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 3, 0.0 , 0 , 1 );";
//    String cssStyle2="-fx-background-color:\n" +
//                "        linear-gradient(#686868 0%, #232723 25%, #373837 75%, #757575 100%),\n" +
//                "        linear-gradient(#020b02, #3a3a3a),\n" +
//                "        linear-gradient(#9d9e9d 0%, #6b6a6b 20%, #343534 80%, #242424 100%),\n" +
//                "        linear-gradient(#8a8a8a 0%, #6b6a6b 20%, #343534 80%, #262626 100%),\n" +
//                "        linear-gradient(#777777 0%, #606060 50%, #505250 51%, #2a2b2a 100%);\n" +
//                "    -fx-background-insets: 0,1,4,5,6;\n" +
//                "    -fx-background-radius: 9,8,5,4,3;\n" +
//                "    -fx-padding: 0 10 0 10;\n" +
//                "    -fx-font-family: \"Helvetica\";\n" +
//                "    -fx-font-size: 18px;\n" +
//                "    -fx-font-weight: bold;\n" +
//                "    -fx-text-fill: white;\n" +
//                "    -fx-effect: dropshadow( three-pass-box , rgba(255,255,255,0.2) , 1, 0.0 , 0 , 1);";
//    String cssStyle3="-fx-background-color:\n" +
//                "        rgba(0,0,0,0.08),\n" +
//                "        linear-gradient(#9a9a9a, #909090),\n" +
//                "        linear-gradient(white 0%, #f3f3f3 50%, #ececec 51%, #f2f2f2 100%);\n" +
//                "    -fx-background-insets: 0 0 -1 0,0,1;\n" +
//                "    -fx-background-radius: 5,5,4;\n" +
//                "    -fx-padding: 3 30 3 30;\n" +
//                "    -fx-text-fill: #242d35;\n" +
//                "    -fx-font-size: 14px;";
    public MyMenu(int width,int height,Color color){
        this.width=width;
        this.height=height;        
        this.color=color;
        this.setPreferredSize(new Dimension(width, height));
        
    }
    public void addController(MyController controller){
        this.controller=controller;
    }
    public void addBrain(MyBrain brain){
        this.brain=brain;
    }
    public void createMenu(){
        
        final MenuBar menuBar=new MenuBar();
        Menu file=new Menu("文件(F)");
        MenuItem newdbItem=new MenuItem("新建工程文件");
        MenuItem openItem=new MenuItem("打开数据库文件");
        MenuItem openExcelItem=new MenuItem("打开Excel表格文件");
        MenuItem saveItem=new MenuItem("保存");
        MenuItem saveasItem=new MenuItem("另存为");
        MenuItem closeItem=new MenuItem("关闭");
//        file.setStyle(cssStyle1);
//        newdbItem.setStyle(cssStyle3);
//        openItem.setStyle(cssStyle3);
//        openExcelItem.setStyle(cssStyle3);
//        saveItem.setStyle(cssStyle3);
//        saveasItem.setStyle(cssStyle3);
//        closeItem.setStyle(cssStyle3);
        file.getItems().add(newdbItem);
        file.getItems().add(openItem);
        file.getItems().add(openExcelItem);
        file.getItems().add(saveItem);
        file.getItems().add(saveasItem);
        file.getItems().add(new SeparatorMenuItem());
        newdbItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                try {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("创建新3D数据文件");
                    fileChooser.getExtensionFilters().add(
                            new ExtensionFilter("3D project SQLite file", "*.xml"));
                    File newFile = fileChooser.showSaveDialog(null);
                    if (newFile != null) {  
                        controller.getFxStatus().updateStatus("正在创建3D数据文件", "光标当前位置"+t.getTarget(), 
                                "打开文件的名称"+newFile.getName(), newFile.getCanonicalPath());
                        controller.setDbfilepath(newFile.getAbsolutePath());                                                
                        controller.makeNewFile(newFile.getAbsolutePath());
                        
                    }
                    
                } catch (Exception ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        openItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                try {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("选择你要导入的数据库文件");
                    fileChooser.getExtensionFilters().add(
                            new ExtensionFilter("3D project xml file", "*.xml"));
                    File selectedFile = fileChooser.showOpenDialog(null);
                    if (selectedFile != null) {
                        controller.getFxStatus().updateStatus("正在打开文件", "光标当前位置"+t.getTarget(), 
                                "打开文件的名称"+selectedFile.getName(), selectedFile.getAbsolutePath());
                        controller.doLoadFile(selectedFile.getAbsolutePath());
                        
                    }
                    
                } catch (Exception ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        openExcelItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                try {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("选择你要导入的Excel文件,格式为xls或xlsx");
                    fileChooser.getExtensionFilters().add(
                            new ExtensionFilter("Excel all format", "*.xls","*.xlsx"));
                    File selectedFile = fileChooser.showOpenDialog(null);
                    if(selectedFile!=null){                        
                        controller.getFxStatus().updateStatus("正在打开文件", "光标当前位置"+t.getTarget(), "打开文件的名称"+selectedFile.getName(), selectedFile.getAbsolutePath());
                        controller.setExcelpath(selectedFile.getAbsolutePath());   
                        controller.doLoadExcelFile(selectedFile.getAbsolutePath());
                        
                    }
                    
                } catch (Exception ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        saveasItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                try {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("3D数据库文件另存为");
                    fileChooser.getExtensionFilters().add(
                            new ExtensionFilter("3D project xml file or Microsoft stylesheet file", "*.xlsx","*.xml"));
                    File outFile = fileChooser.showSaveDialog(null);
                    if (outFile != null) {
                        controller.getFxStatus().updateStatus("正在保存文件", "光标当前位置"+t.getTarget(), "保存文件的名称"+outFile.getName(), outFile.getAbsolutePath());
                        
                        String extName = outFile.getAbsoluteFile().toString().substring(
                                outFile.getAbsoluteFile().toString().lastIndexOf(".") + 1);
                        
                        if(extName.equals("xlsx")){
                            controller.saveExcelFile(outFile.getAbsolutePath());
                        }else if(extName.equals("xml")){
                            controller.setDbfilepath(outFile.getAbsolutePath());
                            controller.saveFile(outFile.getAbsolutePath());
                        }else{
                            controller.saveExcelFile(outFile.getAbsolutePath());
                        }                       
                        
                    }
                    
                } catch (Exception ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        closeItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                System.exit(0);
            }
        });
        file.getItems().add(closeItem);
        Menu edit=new Menu("编辑(E)");
        MenuItem editzkItem=new MenuItem("编辑钻孔");
        MenuItem editzcItem=new MenuItem("编辑钻场");
        MenuItem editgzmItem=new MenuItem("编辑工作面");
        MenuItem editccItem=new MenuItem("编辑采场");
        edit.getItems().add(editzkItem);
        edit.getItems().add(editzcItem);
        edit.getItems().add(editgzmItem);
        edit.getItems().add(new SeparatorMenuItem());
        edit.getItems().add(editccItem);
//        edit.setStyle(cssStyle1);
        Menu gui=new Menu("视图(V)");
        MenuItem caidanlanItem=new MenuItem("菜单栏显示");
        MenuItem treeShowItem=new MenuItem("树状图显示");
        MenuItem fullScreenItem=new MenuItem("全屏");
        MenuItem only3DItem=new MenuItem("仅3D视图");
//        gui.setStyle(cssStyle1);
        gui.getItems().add(caidanlanItem);
        gui.getItems().add(treeShowItem);
        gui.getItems().add(fullScreenItem);
        gui.getItems().add(only3DItem);
        Menu analy=new Menu("预测(A)");
//        analy.setStyle(cssStyle1);
        MenuItem zuankongjiancejingduTtem=new MenuItem("请输入钻孔检测精度");
        MenuItem zuankongCheckItem=new MenuItem("钻孔探查");
        MenuItem reportItem=new MenuItem("生成报告");
        analy.getItems().add(zuankongjiancejingduTtem);
        analy.getItems().add(zuankongCheckItem);
        analy.getItems().add(reportItem);
        zuankongjiancejingduTtem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                String jingdu=JOptionPane.showInputDialog("请输入钻孔检测精度数值（4-20），\n数值越大精度越高\n但会相应降低程序性能");
                controller.setZuankongjiancejingdu(Integer.parseInt(jingdu));
                controller.getFxStatus().updateStatus("更新终压极限值成功", jingdu.toString() , "编辑终压", t.toString());
            }
        });
        Menu tool=new Menu("工具(T)");
//        tool.setStyle(cssStyle1);
        MenuItem makeDBItem=new MenuItem("创建数据库");
        MenuItem makeDBGUIItem=new MenuItem("创建数据库GUI");
        MenuItem addDataItem=new MenuItem("添加数据小助手");
        MenuItem txtToImgItem=new MenuItem("文字转图片");
        MenuItem snapshotItem=new MenuItem("截图");
        txtToImgItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                try {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("将您的文字创建为图片");
                    fileChooser.getExtensionFilters().add(
                            new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
                    File newFile = fileChooser.showSaveDialog(null);
                    if (newFile != null) {
                        System.out.println(newFile.getAbsoluteFile());
                        String sb=newFile.getAbsoluteFile().toURI().toURL().toString();
                        String filepath=sb.substring(5);
                         controller.getFxStatus().updateStatus("正在创建文字图片", "光标当前位置"+t.getTarget(), 
                                "创建文件的名称"+newFile.getName(), filepath);
                        new StringToImg(newFile.getName(), filepath, 100, 40);
                       
                    }
                    
                } catch (Exception ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        snapshotItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                controller.getFxStatus().updateStatus("正在创建文字图片", "光标当前位置"+t.getTarget(), 
                                "创建文件的名称"+"", "");
                new AWTpicture();
            }
        });
        tool.getItems().add(makeDBItem);
        tool.getItems().add(makeDBGUIItem);
        tool.getItems().add(addDataItem);
        tool.getItems().add(txtToImgItem);
        tool.getItems().add(snapshotItem);
        Menu property=new Menu("参数调节(P)");
//        property.setStyle(cssStyle1);
        MenuItem fangweijiaoItem=new MenuItem("胶带顺槽方位角");
        MenuItem factorItem=new MenuItem("模型比例");
        Menu zkTypeItem=new Menu("钻孔类型");
        final CheckMenuItem zktypecylinder=new CheckMenuItem("柱体");
        final CheckMenuItem zktypecuboid=new CheckMenuItem("长方形");
        zkTypeItem.getItems().add(zktypecylinder);
        zkTypeItem.getItems().add(zktypecuboid);
        zktypecylinder.setSelected(true);
        zktypecylinder.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                zktypecuboid.setSelected(false);
                controller.setZuankongType(AllGlobal.CYLINDER_TYPE);
            }
        });
        zktypecuboid.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                zktypecylinder.setSelected(false);
                controller.setZuankongType(AllGlobal.CUBOID_TYPE);
            }
        });
        MenuItem zhongyashaixuanItem=new MenuItem("终压筛选范围");
        zhongyashaixuanItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                String str=JOptionPane.showInputDialog("请输入最小可用终压值极限");
                if(str==null)
                    controller.getFxStatus().updateStatus("没有更新极限值，数值非法", "null" , "编辑终压", "null");
                else{
                    controller.setZhongyajixian(Float.parseFloat(str));
                    controller.getFxStatus().updateStatus("更新终压极限值成功", str.toString() , "编辑终压", t.toString());
                }
                
            }
        });
        Menu gzmtypeItem=new Menu("工作面位置");
        final CheckMenuItem gzmleftcmi=new CheckMenuItem("左工作面");
        final CheckMenuItem gzmrightcmi=new CheckMenuItem("右工作面");
        gzmtypeItem.getItems().add(gzmleftcmi);
        gzmtypeItem.getItems().add(gzmrightcmi);
        gzmleftcmi.setSelected(true);
        gzmleftcmi.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                if(gzmleftcmi.isSelected()){
                    gzmrightcmi.setSelected(false);
                    controller.setGZM_TYPE(AllGlobal.GZM_TYPE_LEFT);
                }
            }
        });
        gzmrightcmi.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                if(gzmrightcmi.isSelected()){
                    gzmleftcmi.setSelected(false);
                    controller.setGZM_TYPE(AllGlobal.GZM_TYPE_RIGHT);
                }
            }
        });
        property.getItems().add(zhongyashaixuanItem);
        property.getItems().add(zkTypeItem);
        property.getItems().add(fangweijiaoItem);
        property.getItems().add(factorItem);
        property.getItems().add(gzmtypeItem);
        
        Menu about=new Menu("关于(H)");
//        about.setStyle(cssStyle1);
        MenuItem abouItem=new MenuItem("关于");
        MenuItem docItem=new MenuItem("Java doc");
        MenuItem helpItem=new MenuItem("帮助");
        MenuItem updateItem=new MenuItem("检查更新");
        abouItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                new About();
            }
        });
        docItem.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                try {
                    Runtime runtime=Runtime.getRuntime();
                    runtime.exec("cmd.exe /c start "+controller.getDocpath());
                } catch (IOException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        about.getItems().add(abouItem);
        about.getItems().add(helpItem);
        about.getItems().add(docItem);
        about.getItems().add(updateItem);      
        menuBar.getMenus().add(file);
        menuBar.getMenus().add(edit);
        menuBar.getMenus().add(gui);
        menuBar.getMenus().add(analy);
        menuBar.getMenus().add(tool);
        menuBar.getMenus().add(property);
        menuBar.getMenus().add(about);
        Group menuGroup=new Group();
        ImageView back=new ImageView(new Image("myfx/jfxpanel/status_bk_1.png"));
        back.setTranslateY(-3);
        menuGroup.getChildren().add(back);
        menuBar.setPrefSize(width, height);
        menuBar.setOpacity(0.9);
        menuGroup.getChildren().add(menuBar);
        Scene menuscene=new Scene(menuGroup, width, height, color);
        this.setScene(menuscene);
//        String cssStyle="-fx-background-color:#a6b5c9,"
//                + "linear-gradient(#303842 0%, #3e5577 20%, #375074 100%),"
//                + "linear-gradient(#768aa5 0%, #849cbb 5%, #5877a2 50%, #486a9a 51%, #4a6c9b 100%);"
//                + "-fx-background-insets: 0 0 -1 0,0,1;"
//                + "-fx-background-radius: 5,5,4;"
//                + "-fx-padding: 0 30 7 30;"
//                + "-fx-text-fill: #242d35;"
//                + "-fx-font-size: 12px;"
//                + "-fx-text-fill: white;";
//        menuBar.setStyle(cssStyle);
        menuBar.setOnMouseEntered(new EventHandler<MouseEvent>() {

            public void handle(MouseEvent t) {
                menuBar.requestFocus();
            }
        });
    }
}
